<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title am-fl">活动目标设置</div>
        </div>

        <div class="widget-body  am-fr">
          <div class="am-u-sm-8 am-u-md-8 am-u-lg-2">
            <div class="am-form-group">
              <el-input
                v-model="query.activityName"
                placeholder="活动名称"
              >
              </el-input>
            </div>
          </div>
          <div class="am-u-sm-8 am-u-md-8 am-u-lg-2">
            <div class="am-form-group">
              <el-select
                v-model="query.areaTeamId"
                placeholder="请选择地区"
              >
                <el-option
                  v-for="(item, index) in areaTeams"
                  :key="index"
                  :label="item.text"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
            <div class="am-form-group">
              <el-select
                v-model="query.campusId"
                placeholder="意向校区"
                clearable
              >
                <el-option
                  v-for="(item, index) in busTeams"
                  :key="index"
                  :value="item.campusId"
                  :label="item.campusName"
                >
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
            <div class="am-form-group">
              <el-date-picker
                v-model="dateArr"
                type="daterange"
                placeholder="选择日期范围"
              >
              </el-date-picker>
            </div>
          </div>
          <div class="am-u-sm-20 am-u-md-20 am-u-lg-8 am-u-end">
            <div class="am-form-group">
              <button
                @click="handleSearch"
                type="button"
                class="am-btn am-btn-default am-btn-success"
              ><span class="am-icon-search"></span>查询
              </button>
              <button
                @click="goExport"
                type="button"
                class="am-btn am-btn-default am-btn-success"
              >导出
              </button>
            </div>
          </div>
          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              empty-text="暂无数据"
              stripe
              style="min-width: 100%"
            >
              <el-table-column
                min-width="100"
                prop="activityName"
                label="活动名称"
              >
              </el-table-column>
              <el-table-column
                min-width="200"
                prop="campusName"
                label="校区"
              >
              </el-table-column>
              <el-table-column
                min-width="150"
                prop="activityStartDate"
                label="活动时间"
              >
                <template scope="scope">
                  <div>{{scope.row.activityStartDate | formatDate}}</div>
                </template>
              </el-table-column>

              <el-table-column
                min-width="130"
                prop="totalTargetNum"
                label="活动总目标"
              >
                <template scope="scope">
                  <el-input-number
                    style="width:100%"
                    v-if="editData[scope.row.activityTargetId]"
                    :controls="false"
                    v-model="editData[scope.row.activityTargetId].totalTargetNum"
                    :disabled="scope.row.disableEdit"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column
                min-width="130"
                label="新老生目标"
                prop="newOldTargetNum"
              >
                <template scope="scope">
                  <el-input-number
                    style="width:100%"
                    v-if="editData[scope.row.activityTargetId]"
                    :controls="false"
                    :disabled="scope.row.disableEdit"
                    v-model="editData[scope.row.activityTargetId].newOldTargetNum"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column
                min-width="150"
                prop="studentPhoneNo"
                label="目标转化率"
              >
                <template scope="scope">
                  <el-input
                    v-if="editData[scope.row.activityTargetId]"
                    :disabled="scope.row.disableEdit"
                    v-model="editData[scope.row.activityTargetId].tranferRate"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                min-width="150"
                label="是否开启签到"
              >
                <template scope="scope">
                  <div v-if="editData[scope.row.activityTargetId]">
                    <el-select
                      :disabled="scope.row.disableEdit"
                      placeholder="请选择"
                      v-model="editData[scope.row.activityTargetId].signStatus"
                      @input="editDataChange(scope.row)"
                    >
                      <el-option
                        value="1"
                        label="是"
                        key="1"
                      ></el-option>
                      <el-option
                        value="0"
                        label="否"
                        key="0"
                      ></el-option>
                    </el-select>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                min-width="260"
                label="操作"
              >
                <template scope="scope">
                  <div>
                    <el-button
                      v-if="hasPermission('edit')"
                      @click="opeateEdit(scope.row)"
                    >{{scope.row.disableEdit ? '编辑' : '保存'}}</el-button>
                    <el-button
                      class="copybtn"
                      :data-clipboard-text="`${shareUrl}sign?activityId=${scope.row.activityId}`"
                    >复制签到链接</el-button>

                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div class="am-u-lg-12 am-cf">
          <div class="am-fr">
            <pagination
              v-bind:total="total"
              v-bind:pageNo="pageNo"
              v-bind:pageSize="pageSize"
              @paging="loadTableData"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import io from '../../lib/io'
import Pagination from '../base/Pagination.vue'
import moment from 'moment'
import Clipboard from 'clipboard'
import conf from '../../lib/conf'
export default {
  data: function() {
    return {
      total: 0,
      pageSize: 10,
      pageNo: 1,
      tableData: [{}],
      disableEdit: true,
      editData: {},
      query: {
        activityName: '',
        campusId: '',
        areaTeamId: '',
        startDate: '',
        endDate: ''
      },
      activityId: '',
      dateArr: [],
      shareUrl: conf.baseApiPath.replace('yyfn-api', 'activity-h5/#/')
    }
  },
  components: {
    Pagination
  },
  watch: {
    'query.areaTeamId': function(newVal, oldVal) {
      if (!oldVal) {
        this.loadTableData()
      }
      this.query.campusId = ''
    }
  },
  computed: {
    areaTeams() {
      let options = (this.$root.config.areaTeams || []).map(item => {
        if (!this.query.areaTeamId) {
          this.query.areaTeamId = item.areaTeamId
        }
        return { value: item.areaTeamId, text: item.name }
      })
      return options
    },
    busTeams: function() {
      let list = this.$root.config.campusList.filter((item, index) => {
        if (item.areaTeamId === this.query.areaTeamId) {
          return true
        }
        return false
      })
      return list
    }
  },
  created() {},
  methods: {
    editDataChange(row) {
      let index = 0
      for (let i of this.tableData) {
        if (i.activityTargetId === row.activityTargetId) {
          break
        }
        index++
      }
      this.$set(this.tableData, index, this.tableData[index])
    },
    changeStatus(item) {
      let index = 0
      for (let i of this.tableData) {
        if (i.activityTargetId === item.activityTargetId) {
          i.disableEdit = !i.disableEdit
          break
        }
        index++
      }
      this.$set(this.tableData, index, this.tableData[index])
    },
    opeateEdit(item) {
      if (!item.disableEdit) {
        io.post(
          io.changeActivityTargetById,
          {
            ...this.editData[item.activityTargetId],
            activityTargetId: item.activityTargetId
          },
          ret => {
            if (ret.success) {
              this.changeStatus(item)
              // this.loadTableData()
            } else {
              this.$alert(ret.desc)
            }
          }
        )
      } else {
        this.changeStatus(item)
      }
    },
    handleSearch() {
      this.loadTableData(1)
    },
    goExport() {
      io.downloadFile(
        io.exportActivityTarget,
        this.formatData(this.query),
        res => {}
      )
    },
    loadTableData(pageNo) {
      this.pageNo = pageNo || this.pageNo || 1
      this.$showLoading()
      io.post(
        io.findActivityTargetPage,
        Object.assign({}, this.formatData(this.query), {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }),
        ret => {
          this.$hiddenLoading()
          if (ret.success) {
            this.editData = {}
            this.tableData = ret.data.list
            this.tableData = this.tableData.map((item, index) => {
              item.disableEdit = true
              let {
                newOldTargetNum,
                signStatus,
                totalTargetNum,
                tranferRate
              } = item
              this.editData[item.activityTargetId] = {
                signStatus,
                totalTargetNum,
                tranferRate,
                newOldTargetNum
              }
              return item
            })
            this.total = ret.data.total
            this.$nextTick(() => {
              let clipbord = new Clipboard('.copybtn')
              clipbord.on('success', e => {
                this.$alert('复制成功')
              })
            })
          } else {
            this.$alert(ret.desc)
          }
        }
      )
    },
    formatData(data) {
      return {
        activityName: data.activityName,
        campusId: data.campusId,
        areaTeamId: data.areaTeamId,
        startDate:
          this.dateArr.length > 0 && this.dateArr[0]
            ? moment(this.dateArr[0]).format('YYYY-MM-DD')
            : '',
        endDate:
          this.dateArr.length > 0 && this.dateArr[1]
            ? moment(this.dateArr[1]).format('YYYY-MM-DD')
            : ''
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
